import { keyIndex } from "../../../../api/dashboard";
import BaseData from "../baseData";

export default function useKeyIndexData(formState: any, flag: string) {
  let keyIndexData: any;
  if (flag === "screenPage") {
    keyIndexData = JSON.parse(JSON.stringify(BaseData.baseData));
  } else {
    keyIndexData = JSON.parse(JSON.stringify(BaseData.kanbanKeyIndexData));
  }
  return new Promise(resolve => {
    const getKeyIndexData = async () => {
      let startTime = formState.rangedate[0].format("YYYY-MM-DD");
      let endTime = formState.rangedate[1].format("YYYY-MM-DD");
      let data = {
        startTime,
        endTime,
        platformAppCodeStr: formState.applet,
        actIdStr: formState.activities,
        isNeedYesterdayData: 1,
        //事件
        //hdyx_uv  访问人数
        //hdyx_participate_uv 参与人数
        //hdyx_join_uv 入会人数
        eventCodeStr: "hdyx_uv,hdyx_participate_uv,hdyx_join_uv"
      };
      let res = await keyIndex(data);
      if (res?.code === 0) {
        console.log("关键指标", res);
        //遍历赋值
        for (let key in res.data) {
          //访问人数 hdyx_uv
          if (key === keyIndexData[0].id) {
            keyIndexData[0].totalNum = res.data[key].total_num;
            keyIndexData[0].yestodayNum = res.data[key].yesterday_num;
          }
          //参与人数 hdyx_participate_uv
          if (key === keyIndexData[1].id) {
            keyIndexData[1].totalNum = res.data[key].total_num;
            keyIndexData[1].yestodayNum = res.data[key].yesterday_num;
          }
          //入会人数 hdyx_join_uv
          if (key === keyIndexData[2].id) {
            keyIndexData[2].totalNum = res.data[key].total_num;
            keyIndexData[2].yestodayNum = res.data[key].yesterday_num;
          }
        }
        resolve(keyIndexData);
      }
    };
    getKeyIndexData();
  });
  //return keyIndexData
}
